package com.qimou.iems.controller;

import com.qimou.iems.entity.PageArgs;
import com.qimou.iems.entity.PageList;
import com.qimou.iems.entity.QualityItem;
import com.qimou.iems.service.IQualityCategoryChildService;
import com.qimou.iems.service.IQualityItemService;
import com.qimou.iems.util.CommonUtil;
import com.qimou.iems.util.StatusConstant;
import com.qimou.iems.util.ViewData;
import com.qimou.iems.util.ViewDataPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Qimou Xie
 * @date 2018/6/9 14:47.
 **/

@RestController
@RequestMapping(value = "/qualityItem")
public class QualityItemController extends BaseController {


    @Resource
    private IQualityItemService qualityItemService;

    @Resource
    private IQualityCategoryChildService qualityCategoryChildService;


    @RequestMapping(value = "/getQualityItem")
    public ViewData getQualityItem(Integer id){
        QualityItem byId = qualityItemService.findById(id);
        byId.setCategoryChild(qualityCategoryChildService.findById(byId.getChildCategoryId()));
        return buildSuccessJson(StatusConstant.SUCCESS_CODE,"获取成功",
                byId);

    }

    @RequestMapping(value = "/getQualityItemList")
    public ViewData getQualityItemList(Integer childId){
        return buildSuccessJson(StatusConstant.SUCCESS_CODE,"获取成功",
                qualityItemService.findQualityList(childId));

    }



    @RequestMapping(value = "/getList")
    public ViewDataPage getList(PageArgs pageArgs,Integer childCategoryId,String qualityName){

        childCategoryId = CommonUtil.isEmpty(childCategoryId) ? null : childCategoryId;
        qualityName = CommonUtil.isEmpty(qualityName) ? null : qualityName;

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("qualityName",qualityName);
        map.put("childCategoryId",childCategoryId);
        PageList<QualityItem> list = qualityItemService.findList(map, pageArgs);
        return buildSuccessViewDataPage(StatusConstant.SUCCESS_CODE,"获取成功",
                list.getTotalSize(),list.getList());

    }


    @RequestMapping(value = "/save")
    public ViewData save(QualityItem qualityItem){
        if (null == qualityItem.getId()) {
            qualityItemService.save(qualityItem);
        }
        else{
            qualityItemService.update(qualityItem);
        }
        return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"操作成功");
    }


}
